package com.fjp.heartguard.ui.report.adapter

import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fjp.heartguard.R
import com.fjp.heartguard.app.data.bean.GroupQuestionType
import com.fjp.heartguard.app.data.bean.Questionnaire
import com.fjp.heartguard.app.ext.dp
import com.fjp.heartguard.app.ext.setAdapterAnimation
import com.fjp.heartguard.app.util.SettingUtil
import com.fjp.heartguard.app.weight.pop.TipPop
import com.fjp.heartguard.app.weight.seekbar.HTSeekBar
import com.fjp.heartguard.app.weight.seekbar.HTSeekBarLab
import com.lxj.xpopup.XPopup


class ReportDetailAdapter(data: MutableList<Questionnaire>, var clickItem: (item: Questionnaire) -> Unit) : BaseQuickAdapter<Questionnaire, BaseViewHolder>(R.layout.item_report_detail, data) {
    val colors = arrayListOf("#59D488", "#D9D44B", "#FF6600", "#CB392B", "#9A281E")

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(holder: BaseViewHolder, item: Questionnaire) {
        val list = arrayListOf<HTSeekBarLab>()
        holder.setText(R.id.scroeTv, "${item.score} 分")
        holder.setText(R.id.titleTv, item.title)
        if(item.type == GroupQuestionType.COGNITION_B.value){
            holder.setGone(R.id.scroeTv, true)
        }else{
            holder.setGone(R.id.scroeTv, false)
        }
        if(item.type ==  GroupQuestionType.DAILY_LIFE_A.value || item.type ==  GroupQuestionType.DAILY_LIFE_B.value){
            holder.setText(R.id.titleTv,"日常生活活动能力评估")
            holder.setText(R.id.subTitle,item.title)
            holder.setVisible(R.id.subTitle,true)
        }else{
            holder.setGone(R.id.subTitle,true)
        }
        when (item.type) {
            GroupQuestionType.COGNITION_A.value -> {
//                若总得分为0-7分，为重度认知障碍；8-12分为中度认知障碍；13-15分为认知水平正常
                list.add(HTSeekBarLab(colors[0], 2, "正常"))
                list.add(HTSeekBarLab(colors[1], 4, "中度"))
                list.add(HTSeekBarLab(colors[2], 6, "严重"))
                val index = if (item.score <= 7) {
                    5
                } else if (item.score <= 12) {
                    3
                } else {
                    1
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, index)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_01)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_01))

            }

            GroupQuestionType.COGNITION_B.value -> {
//                按照第8题答案，给出认知功能正常或轻度、中度、重度认知障碍
                list.add(HTSeekBarLab(colors[0], 2, "正常"))
                list.add(HTSeekBarLab(colors[1], 4, "轻度"))
                list.add(HTSeekBarLab(colors[2], 6, "中度"))
                list.add(HTSeekBarLab(colors[3], 8, "严重"))
                var score = 0
                if (item.result == "认知功能正常") {
                    score = 1
                } else if (item.result == "轻度认知障碍") {
                    score = 3
                } else if (item.result == "中度认知障碍") {
                    score = 5
                } else {
                    score = 7
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_01)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_01))

            }
//            13-16分：移动能力良好，基本独立
//            9-12分：移动能力轻度受限，可能需要少量帮助
//            5-8分：移动能力中度受限，需要中等程度帮助
//            0-4分：移动能力严重受限，需要大量帮助或完全依赖
            GroupQuestionType.LOCOMOTIVITY.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "正常"))
                list.add(HTSeekBarLab(colors[1], 4, "轻度"))
                list.add(HTSeekBarLab(colors[2], 6, "中度"))
                list.add(HTSeekBarLab(colors[3], 8, "严重"))

                val score = if (item.score <= 4) {
                    7
                } else if (item.score <= 8) {
                    5
                } else if (item.score <= 12) {
                    3
                }else {
                    1
                }

                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_02)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_02))

            }

            GroupQuestionType.NUTRITIONAL_STATUS.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "正常"))
                list.add(HTSeekBarLab(colors[1], 4, "存在风险"))
                list.add(HTSeekBarLab(colors[2], 6, "营养不良"))
                val score = if (item.score <= 1) {
                    1
                } else if (item.score <= 3) {
                    3
                }else {
                    5
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_02))
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_03)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_01))
            }

            GroupQuestionType.SENSORY_ABILITY.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "正常"))
                list.add(HTSeekBarLab(colors[1], 4, "轻度"))
                list.add(HTSeekBarLab(colors[2], 6, "中度"))
                list.add(HTSeekBarLab(colors[3], 8, "严重"))
                val score = if (item.score <= 2) {
                    1
                } else if (item.score <= 5) {
                    3
                } else if (item.score <= 8) {
                    5
                }else {
                    7
                }

                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_04)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_02))
            }

            GroupQuestionType.DEPRESSED.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "倾向较低"))
                list.add(HTSeekBarLab(colors[1], 4, "可能抑郁"))
                val score = if (item.score <3) {
                    1
                } else {
                    3
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_05)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_01))
            }

            GroupQuestionType.DEFECATION.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "正常"))
                list.add(HTSeekBarLab(colors[1], 4, "轻度"))
                list.add(HTSeekBarLab(colors[2], 6, "重度"))
                list.add(HTSeekBarLab(colors[3], 8, "极重度"))
                val score = if (item.score ==0) {
                    1
                } else if (item.score <= 3) {
                    3
                } else if (item.score <= 6) {
                    5
                }else {
                    7
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list,score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_06)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_02))
            }

            GroupQuestionType.DAILY_LIFE_A.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "独立"))
                list.add(HTSeekBarLab(colors[1], 4, "轻度依赖"))
                list.add(HTSeekBarLab(colors[2], 6, "中度依赖"))
                list.add(HTSeekBarLab(colors[3], 8, "严重依赖"))
                list.add(HTSeekBarLab(colors[4], 10, "完全依赖"))
                val score = if (item.score ==100) {
                    1
                } else if (item.score >=91) {
                    3
                } else if (item.score >= 61) {
                    5
                }else if (item.score >=21) {
                    7
                }else {
                   9
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_07)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_01))

            }

            GroupQuestionType.DAILY_LIFE_B.value -> {
                list.add(HTSeekBarLab(colors[0], 2, "轻度或独立"))
                list.add(HTSeekBarLab(colors[1], 4, "中度"))
                list.add(HTSeekBarLab(colors[2], 6, "严重"))
                val score = if (item.score >=6) {
                    1
                } else if (item.score >= 3) {
                    3
                }else {
                    5
                }
                holder.itemView.findViewById<HTSeekBar>(R.id.seekBar).setLabs(list, score)
                holder.setImageResource(R.id.iconIv, R.drawable.report_type_07)
                holder.setBackgroundColor(R.id.bottomLine, context.resources.getColor(R.color.report_type_01))
            }


            else -> {}
        }
        holder.setText(R.id.desTv, item.result)
        holder.itemView.setOnClickListener {
            clickItem(item)
        }
        holder.itemView.findViewById<ImageView>(R.id.tipIv).setOnClickListener {
            showTipDialog(item.title, item.description)
        }
    }

    private fun showTipDialog(title: String, des: String) {
        XPopup.Builder(context).dismissOnTouchOutside(false).dismissOnBackPressed(false).isDestroyOnDismiss(true).autoOpenSoftInput(false).popupWidth(400.dp.toInt()).asCustom(TipPop(context, title, des)).show()
    }

}


